package com.yang.zngcxyxkjs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.zngcxyxkjs.mapper.CompetitionMapper;
import com.yang.zngcxyxkjs.mapper.StudentMapper;
import com.yang.zngcxyxkjs.pojo.Competition;
import com.yang.zngcxyxkjs.pojo.LoginLog;
import com.yang.zngcxyxkjs.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CompetitionMapper competitionMapper;

    @RequestMapping("/studentlogin")
    public String tostudentLogin(){
        return "student/studentLogin.html";
    }

    @RequestMapping("/stuLogin")
    public String stuLogin(String stuNo, String password, HttpSession session,Model model){
        Student stu = studentMapper.studentLogin(stuNo,password);
        if(stu!=null){
            LoginLog loginlog = new LoginLog();
            loginlog.setLoginAccount(stuNo);
            studentMapper.insertStudentLoginlog(loginlog);
            session.setAttribute("stu",stu);
            model.addAttribute("stu",stu);
            return "student/studentIndex.html";
        }else{
            return "error.html";
        }
    }


    @RequestMapping("/getStudentByStuNo")
    public String getStudentByStuNo(String stuNo,Model model){
        Student stu = studentMapper.selectStudentByStuNo(stuNo);
        model.addAttribute("stu",stu);

        return "student/studentInfo.html";
    }

    @RequestMapping("/updateStudentById")
    public String updateStudentById(Student student,Model model){
        boolean i = studentMapper.updateStudent(student);
        model.addAttribute("i",i);
        if(i){
            model.addAttribute("stu",student);
            return "student/studentInfo.html";
        }else{
            return "error.html";
        }
    }

    @RequestMapping("/register")
    public String register(){
        return "student/studentRegister.html";
    }

    @RequestMapping("/insertStudent")
    public String register(Student student){
        boolean b = studentMapper.insertStudent(student);
        if(b){
            return "student/studentLogin.html";
        }else{
            return "error.html";
        }
    }

    @RequestMapping("/toAchievement")
    public String toAchievement(Model model, HttpSession session){
        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);
        return "student/studentAchievement.html";
    }

    @RequestMapping("/toCeShi")
    public String toCeShi(){
        return "ceshi.html";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/studentlogin";
    }

    @RequestMapping("/stugetAllCompetitionsByPages")
    public String getAllCompetitions(@RequestParam(defaultValue = "1") Integer currentPageNum, Model model, HttpSession session, Integer f){
        PageHelper.startPage(currentPageNum,5);
        List<Competition> list = competitionMapper.selectAllCompetitions();
        PageInfo<Competition> pageInfo = new PageInfo<>(list);

        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);

        model.addAttribute("f",f);

        model.addAttribute("pageInfo",pageInfo);
        return "student/studentAllCompetitions.html";
    }

}
